package com.nf.action;

import com.nf.entity.Card;
import com.nf.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@SessionAttributes("card")
public class CardAction {

    @Autowired
    private CardService cardService;

    //ModelandView方法
    @RequestMapping("card")
    public ModelAndView listCard() {
        //创建一个模型视图对象
        ModelAndView mav = new ModelAndView();
        // 获取到查询的数据
        List<Card> cards= cardService.getAllCardInfo();
        //将数据放置到ModelAndView对象view中,第二个参数可以是任何java类型
        mav.addObject("cardList",cards);
        // 放入jsp路径
        mav.setViewName("card");
        // 返回ModelAndView对象
        return  mav;
    }

    //登录
    @RequestMapping("/Denglu")
    public String test() {
        return "login";
    }

    @RequestMapping("/panduan")
    public String login(Card card, Model model) {
        //判断用户是否登陆成功
        card = cardService.login(card.getCardId(), card.getCardPassword());
        if (card == null) {
            model.addAttribute("message", "LOGIN_ERROR");
            return "redirect:Denglu";
        } else {
            model.addAttribute("card", card);
            return "success";
        }
    }


    @RequestMapping("/success")
    public String success(@ModelAttribute("card") Card card,Model model){

        return "success";
    }

    //查询余额
    @RequestMapping("/querybanlance")
    public String queryBanlance(@ModelAttribute("card") Card card, Model model){
        Float banlance = cardService.queryBanlanceBycardId(card.getCardId());
        model.addAttribute("banlance",banlance);
        return "banlance";
    }



    @RequestMapping("/saveMoneyView")
    public String saveMoneyView(@ModelAttribute("card") Card card){

        return "saveMoneyView";
    }

    @RequestMapping("/saveMoney")
    public String saveMoney(@ModelAttribute("card") Card card,Float money){

        boolean flag = cardService.saveMoney(money,card.getCardId());
        if(flag) {
            return "saveMoney_success";
        }else {
            return "saveMoney_error";
        }
    }

}
